﻿using System;
using InseSoft.Infraestructure;
using InseSoft.ViewModels;

namespace InseSoft.Models.Entities
{
    public class EntityFactory
    {
        public static T CreateCatalogEntity<T>()
        {
            var instance = Activator.CreateInstance<T>();
            
            var catalogEntityWithCia = instance as CatalogEntityWithCia;
            if (catalogEntityWithCia != null)
            {
                var session = DIFactory.Get<ApplicationSession>();
                catalogEntityWithCia.Empresa = session.Empresa;
                catalogEntityWithCia.EmpresaId = session.Empresa.Id;
                return (T)Convert.ChangeType(catalogEntityWithCia, typeof(T));
            }

            var entityWithCia = instance as EntityWithCia;
            if (entityWithCia != null)
            {
                var session = DIFactory.Get<ApplicationSession>();
                entityWithCia.Empresa = session.Empresa;
                entityWithCia.EmpresaId = session.Empresa.Id;
                return (T)Convert.ChangeType(entityWithCia, typeof(T));
            }

            return instance;
        }
    }
}
